package com.adams.app.ui;

//Download by http://www.codefans.net
import java.util.ArrayList;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import com.adams.app.R;
import com.adams.app.data.GsonRequest;
import com.adams.app.data.RequestManager;
import com.adams.app.model.Picture;
import com.adams.app.ui.view.ZoomImageView;
import com.adams.app.utils.CommonUtils;
import com.adams.app.vendor.HappyGyApi;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader.ImageContainer;
import com.android.volley.toolbox.ImageLoader.ImageListener;

/**
 * 查看大图的Activity界面。
 * 
 * @author guolin
 */
public class ImageDetailsActivity extends ActionBarActivity implements
		OnPageChangeListener {

	/**
	 * 用于管理图片的滑动
	 */
	private ViewPager viewPager;

	/**
	 * 显示当前图片的页数
	 */
	private TextView pageText;
	private long id;
	private ArrayList<Picture> listUrl;
	private int imagePosition;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.image_details);

		id = getIntent().getExtras().getLong("id");

		imagePosition = getIntent().getIntExtra("image_position", 0);
		pageText = (TextView) findViewById(R.id.page_text);
		viewPager = (ViewPager) findViewById(R.id.view_pager);
		loadData();
	}

	public void loadData() {
		executeRequest(new GsonRequest<Picture.PictureRequestData>(
				String.format(HappyGyApi.IMAGE_LIST, id),
				Picture.PictureRequestData.class, null,
				new Response.Listener<Picture.PictureRequestData>() {
					@Override
					public void onResponse(
							final Picture.PictureRequestData requestData) {
						CommonUtils
								.executeAsyncTask(new AsyncTask<Object, Object, Object>() {
									@Override
									protected Object doInBackground(
											Object... params) {
										return requestData;
									}

									@Override
									protected void onPostExecute(Object o) {
										super.onPostExecute(o);
										listUrl = ((Picture.PictureRequestData) o)
												.getPictures();
										loadItems();
									}
								});
					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError volleyError) {
						Toast.makeText(ImageDetailsActivity.this,
								R.string.refresh_list_failed,
								Toast.LENGTH_SHORT).show();
					}
				}));
	}

	public void loadItems() {
		// 设定当前的页数和总页数
		pageText.setText((imagePosition + 1) + "/" + listUrl.size());
		ViewPagerAdapter adapter = new ViewPagerAdapter();
		viewPager.setAdapter(adapter);
		viewPager.setCurrentItem(imagePosition);
		viewPager.setOnPageChangeListener(this);
		viewPager.setEnabled(false);
	}

	@Override
	public void onStop() {
		super.onStop();
		RequestManager.cancelAll(this);
	}

	protected void executeRequest(Request<?> request) {
		RequestManager.addRequest(request, this);
	}

	/**
	 * ViewPager的适配器
	 * 
	 * @author guolin
	 */

	class ViewPagerAdapter extends PagerAdapter {

		Bitmap mDefaultImageDrawable = BitmapFactory.decodeResource(
				getResources(), R.drawable.empty_photo);

		@Override
		public Object instantiateItem(ViewGroup container, int position) {

			View view = LayoutInflater.from(ImageDetailsActivity.this).inflate(
					R.layout.zoom_image_layout, null);
			ZoomImageView zoomImageView = (ZoomImageView) view
					.findViewById(R.id.zoom_image_view);

			setImageBitmap(HappyGyApi.BASE_URL
					+ listUrl.get(position).getUrlPic(), zoomImageView);
			container.addView(view);
			return view;
		}

		@Override
		public int getCount() {
			return listUrl.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			View view = (View) object;
			container.removeView(view);
		}

		// resize the image proportionately so it fits the entire space
		private void setImageBitmap(String url, final ZoomImageView imageView) {
			RequestManager.loadImage(url, new ImageListener() {

				@Override
				public void onErrorResponse(VolleyError error) {
					if (error != null) {
						imageView.setImageBitmap(mDefaultImageDrawable);
					}
				}

				@Override
				public void onResponse(ImageContainer response,
						boolean isImmediate) {
					if (response.getBitmap() != null) {
						if (!isImmediate && mDefaultImageDrawable != null) {
							imageView.setImageBitmap(response.getBitmap());
						} else {
							imageView.setImageBitmap(response.getBitmap());
						}
					} else if (mDefaultImageDrawable != null) {
						imageView.setImageBitmap(mDefaultImageDrawable);
					}
				}
			});
		}

	}

	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	@Override
	public void onPageSelected(int currentPage) {
		// 每当页数发生改变时重新设定一遍当前的页数和总页数
		pageText.setText((currentPage + 1) + "/" + listUrl.size());
	}
}